/**
 * @Author: SevDaisy十七散人 
 * @Date: 2020-07-05 16:29:00
 */
package zyy.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import zyy.model.BeanAdminUserinfo;
import zyy.util.BaseException;
import zyy.util.BusinessException;
import zyy.util.Bus;

public class FrmAdminUser extends JDialog implements ActionListener {
  private static final long serialVersionUID = -4430365204529797431L;

  private JPanel list = new JPanel();
  private Object tblVUserTitles[] = BeanAdminUserinfo.tblAdminTitles;
  private Object tblVUserData[][];
  DefaultTableModel tabVUserModel = new DefaultTableModel();
  private JTable dataTableVUser = new JTable(tabVUserModel);
  List<BeanAdminUserinfo> allVUser = null;

  private JButton add = new JButton("添加用户");
  private JButton del = new JButton("删除用户");
  private JButton vip = new JButton("设置VIP");
  private JButton exit = new JButton("退出");
  private JPanel btns = new JPanel();

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.add) {
      System.out.println("管理员 添加用户 ");
      // 其实就是用户注册，所以直接唤起用户注册界面
      FrmUserReg dlg_UserReg = new FrmUserReg(this, "用户注册", true, "用户");
      // dlg_UserReg.setLocationRelativeTo(null);s
      dlg_UserReg.setVisible(true);
      this.reloadVUserTable();
    } else if (e.getSource() == this.del) {
      System.out.printf("管理员 删除用户 %s:%d", Bus.curVAdminUser.getuName(), Bus.curVAdminUser.getOrderCnt());
      try {
        Bus.userManager.del(Bus.curVAdminUser);
        this.reloadVUserTable();
      } catch (BusinessException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
      }
    } else if (e.getSource() == this.vip) {
      System.out.println("管理员 设置用户VIP");
      new FrmVipTime("管理员");
    } else if (e.getSource() == this.exit) {
      System.out.println("管理员 退出用户管理");
      Bus.fAdminUser.setVisible(false);
    }
  }

  public void reloadVUserTable() {
    try {
      allVUser = Bus.adminManager.loadAllUser();
    } catch (BaseException e) {
      JOptionPane.showMessageDialog(null, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
      return;
    }
    tblVUserData = new Object[allVUser.size()][tblVUserTitles.length];
    for (int i = 0; i < allVUser.size(); i++) {
      for (int j = 0; j < tblVUserTitles.length; j++)
        tblVUserData[i][j] = allVUser.get(i).getCell(j);
    }
    tabVUserModel.setDataVector(tblVUserData, tblVUserTitles);
    // 限定列宽必须在reload里写死
    this.dataTableVUser.validate();
    this.dataTableVUser.repaint();
    System.out.println("成功重载 View_Admin_User 信息");
  }

  public FrmAdminUser() {
    // 基础 JDialog 设置
    {
      this.setTitle("外卖助手 管理员 用户管理");
      this.setSize(1318, 838);
      this.setLocationRelativeTo(null);
      // this.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          // System.exit(0);
          Bus.fAdminUser.setVisible(false);
        }
      });
      this.setVisible(true);
    }
    // this.getContentPane() 一级布局
    {
      this.getContentPane().setLayout(new BorderLayout());
      this.getContentPane().add(this.list, BorderLayout.CENTER);
      this.getContentPane().add(this.btns, BorderLayout.SOUTH);
      list.setLayout(new BorderLayout());
      list.add(new JScrollPane(this.dataTableVUser), BorderLayout.CENTER);

      // 表格列宽自适应(其实这一行，没觉得有什么作用)
      // 一定不能选 AUTO_RESIZE_OFF 就好了
      dataTableVUser.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
    }
    // btns 相关配置
    {
      this.btns.setLayout(new FlowLayout(FlowLayout.RIGHT));
      this.btns.add(this.add);
      this.btns.add(this.del);
      this.btns.add(this.vip);
      this.btns.add(this.exit);
      this.add.addActionListener(this);
      this.del.addActionListener(this);
      this.vip.addActionListener(this);
      this.exit.addActionListener(this);
      Bus.jButtonZYYStyle(this.add);
      Bus.jButtonZYYStyle(this.del);
      Bus.jButtonZYYStyle(this.vip);
      Bus.jButtonZYYStyle(this.exit);
    }
    // JTable 监听设置
    {
      this.dataTableVUser.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
          int i = FrmAdminUser.this.dataTableVUser.getSelectedRow();
          if (i < 0) {
            return;
          }
          FrmAdminUser.this.reloadVUserTable();
          {
            Bus.curVAdminUser = FrmAdminUser.this.allVUser.get(i);
            System.out.println("选中用户：" + Bus.curVAdminUser);
            FrmAdminUser.this.dataTableVUser.setRowSelectionInterval(i, i);
          }
        }
      });
    }
    // 内容初始化
    {
      this.reloadVUserTable();
      System.out.println("AdminUser 共有列数：" + dataTableVUser.getColumnModel().getColumnCount());
    }
  }
}